package clientApp;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import java.util.ArrayList;

import javax.swing.AbstractListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import communication.client.PlayerInteractionManager;
import communication.common.CommonData;
import communication.common.LoggedPlayers;

import game.map.Static;

import game.units.Character;

import clientApp.utils.Game_Container;
import clientApp.utils.RPG_Workstation_Container;

/**
 * 
 * @author Gabriel
 */
public class InGameScreenPlayer extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;

	private Character character;
	private RPG_Workstation_Container container;
	private boolean inv = false;

	private PlayerInteractionManager player = null;
	private LoggedPlayers loggedPlayers = null;

	public InGameScreenPlayer(RPG_Workstation_Container rpgWorkstationContainer, PlayerInteractionManager player) {
		this.player = player;
		container = rpgWorkstationContainer;
		loggedPlayers = player.getLoggedPlayers();

		this.character = player.getCharacter();
		initComponents();
		initChatBox();
		initPartyMenu();
		initCharStats();
		jPGame.panels.show(jPGame, "Game");
		CharacterBuilder7 comp = new CharacterBuilder7(container, character, null, false);
		comp.characterSheet = true;
		comp.saveButtontext();
		container.add(comp, "CharacterBuilder7");
		jPGame.setInvMenu(new InventoryMenu(jPGame, character));
		if (player.isItMyTurn())
			itsYourTurn();
	}

	private void initCharStats() {
		character.setCurrentHP(character.getLifePoints());
		jLabelCharName.setText(character.getName());
		jLabelCurrentHP.setText("" + character.getCurrentHP());
		jLabelTotalHP.setText("| " + character.getLifePoints());
	}

	private void initPartyMenu() {
		JPanel backPanel = new JPanel();
		backPanel.setBackground(new java.awt.Color(0, 0, 102));
		ArrayList<PartyPanel> panels = new ArrayList<PartyPanel>();
		
		for (int i = 0; i < loggedPlayers.getNumPlayers(); i++) {
			if (!loggedPlayers.get(i).getName().equals(character.getName()))
				panels.add(new PartyPanel(i, loggedPlayers.get(i).getName(), loggedPlayers.get(i).getHp(), loggedPlayers.get(i).getColor()));
		}
		for (int i = loggedPlayers.getNumPlayers(); i < 6; i++)
			panels.add(new PartyPanel(i, "Open Slot " + i, 0));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(backPanel);
		backPanel.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(panels.get(0), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(panels.get(1), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(panels.get(2), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(panels.get(3), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(panels.get(4), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(panels.get(0), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(panels.get(1), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(panels.get(2), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(panels.get(3), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(panels.get(4), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

		jSPParty.setViewportView(backPanel);
	}

	private void initChatBox() {
		cBP = new ChatBoxPanel();
		cBP.setName("Chat");
		jTPChatBox.add(cBP);
	}

	@SuppressWarnings("serial")
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jListChat = new javax.swing.JList();
		jTPTools = new javax.swing.JTabbedPane();
		jPanelPlayerMenu = new javax.swing.JPanel();
		jPStatus = new javax.swing.JPanel();
		jLabelCharName = new javax.swing.JLabel();
		jLabelHP = new javax.swing.JLabel();
		jLabelCurrentHP = new javax.swing.JLabel();
		jLabelTotalHP = new javax.swing.JLabel();
		jPMenus = new javax.swing.JPanel();
		jBInventory = new javax.swing.JButton();
		jBEndTurn = new javax.swing.JButton();
		jBCharSheet = new javax.swing.JButton();
		jSeparator1 = new javax.swing.JSeparator();
		jSPParty = new javax.swing.JScrollPane();
		jTFChat = new javax.swing.JTextField();
		jBSend = new javax.swing.JButton();
		jTPChatBox = new javax.swing.JTabbedPane();
		jPGame = new Game_Container(container, true);

		setBackground(new java.awt.Color(0, 0, 102));
		setMinimumSize(new java.awt.Dimension(800, 600));
		
		jListChat.setModel(new AbstractListModel() {
			String self = player.getCharacter().getName();
			String[] strings = loggedPlayers.getPlayersChatList(self).toArray(new String[loggedPlayers.getNumPlayers()]);

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		jListChat.setSelectedIndex(jListChat.getModel().getSize() - 1);
		jScrollPane1.setViewportView(jListChat);
		
		jTPTools.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));

		jPanelPlayerMenu.setBackground(new java.awt.Color(0, 0, 102));
		jTPTools.addTab("Character", jPanelPlayerMenu);

		jPStatus.setBackground(new java.awt.Color(0, 0, 102));
		jPStatus.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Status", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14), new java.awt.Color(255, 255, 255))); // NOI18N
		jPStatus.setForeground(new java.awt.Color(255, 255, 255));

		jLabelCharName.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabelCharName.setForeground(new java.awt.Color(Static.CYAN));

		jLabelHP.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabelHP.setForeground(new java.awt.Color(255, 255, 255));
		jLabelHP.setText("HP:");

		jLabelCurrentHP.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabelCurrentHP.setForeground(new java.awt.Color(255, 255, 255));
		jLabelCurrentHP.setText("hps");

		jLabelTotalHP.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabelTotalHP.setForeground(new java.awt.Color(255, 255, 255));
		jLabelTotalHP.setText("|  hps");

		javax.swing.GroupLayout jPStatusLayout = new javax.swing.GroupLayout(jPStatus);
		jPStatus.setLayout(jPStatusLayout);
		jPStatusLayout.setHorizontalGroup(jPStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPStatusLayout.createSequentialGroup().addContainerGap().addGroup(jPStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabelCharName).addGroup(jPStatusLayout.createSequentialGroup().addComponent(jLabelHP).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabelCurrentHP).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabelTotalHP))).addContainerGap(46, Short.MAX_VALUE)));
		jPStatusLayout.setVerticalGroup(jPStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPStatusLayout.createSequentialGroup().addContainerGap().addComponent(jLabelCharName).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(jPStatusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabelHP).addComponent(jLabelCurrentHP).addComponent(jLabelTotalHP)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

		jPMenus.setBackground(new java.awt.Color(0, 0, 102));
		jPMenus.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Menus", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14), new java.awt.Color(255, 255, 255))); // NOI18N
		jPMenus.setForeground(new java.awt.Color(255, 255, 255));

		jBInventory.setText("Inventory");
		jBInventory.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jBInventoryActionPerformed(evt);
			}
		});
		
		jBEndTurn.setText("Call End of Turn");
		jBEndTurn.setEnabled(false);
		jBEndTurn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jBEndTurnActionPerformed(evt);
			}
		});

		jBCharSheet.setText("Character");
		jBCharSheet.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jBCharSheetActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPMenusLayout = new javax.swing.GroupLayout(jPMenus);
		jPMenus.setLayout(jPMenusLayout);
		jPMenusLayout.setHorizontalGroup(jPMenusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPMenusLayout.createSequentialGroup()
						.addContainerGap()
						.addGroup(jPMenusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
								.addComponent(jBInventory, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
								.addComponent(jBCharSheet, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
								.addComponent(jBEndTurn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
						.addContainerGap()));
		jPMenusLayout.setVerticalGroup(jPMenusLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPMenusLayout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jBCharSheet)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jBInventory)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jBEndTurn)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

		jBCharSheet.getAccessibleContext().setAccessibleName("jBCharacterSheet");

		jSPParty.setBackground(new java.awt.Color(0, 0, 102));
		jSPParty.setForeground(new java.awt.Color(0, 0, 102));
		jSPParty.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		javax.swing.GroupLayout jPanelPlayerMenuLayout = new javax.swing.GroupLayout(jPanelPlayerMenu);
		jPanelPlayerMenu.setLayout(jPanelPlayerMenuLayout);
		jPanelPlayerMenuLayout.setHorizontalGroup(jPanelPlayerMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanelPlayerMenuLayout.createSequentialGroup().addContainerGap().addGroup(jPanelPlayerMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jPMenus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)).addContainerGap()).addComponent(jSPParty, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE));
		jPanelPlayerMenuLayout.setVerticalGroup(jPanelPlayerMenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanelPlayerMenuLayout.createSequentialGroup().addContainerGap().addComponent(jPMenus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jPStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jSPParty, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE).addContainerGap()));

		jBSend.setText("Send");
		jBSend.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jBSendActionPerformed();
			}
		});
		
		jTFChat.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {}
			@Override
			public void keyReleased(KeyEvent e) {}
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
					if(jTFChat.hasFocus())
						jBSendActionPerformed();				
			}
		});

		jPGame.setPreferredSize(new java.awt.Dimension(585, 488));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTPTools, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTPChatBox, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE).addGroup(layout.createSequentialGroup().addComponent(jBSend).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jTFChat, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)).addComponent(jPGame, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jPGame, javax.swing.GroupLayout.PREFERRED_SIZE, 509, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jTPChatBox, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE).addGap(43, 43, 43)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jTPTools, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jTFChat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jBSend))).addContainerGap()))));

		jPGame.requestFocus();
	}

	private void jBInventoryActionPerformed(java.awt.event.ActionEvent evt) {
		inv = !inv;
		jPGame.setInvMenu(new InventoryMenu(jPGame, character));
		if (inv) {
			jPGame.panels.show(jPGame, "Inventory");
		} else {
			jPGame.panels.show(jPGame, "Game");
		}

	}

	private void jBCharSheetActionPerformed(java.awt.event.ActionEvent evt) {
		CharacterBuilder7 cha7 = new CharacterBuilder7(container, character, null, false);
		cha7.characterSheet = true;
		cha7.saveButtontext();
		container.add(cha7, "CharacterBuilder7");
		container.panels.show(container, "CharacterBuilder7");
	}
	
	private void jBEndTurnActionPerformed(java.awt.event.ActionEvent evt) {
		if(JOptionPane.showConfirmDialog(null,
										"Are you sure you want to call the end of your turn?",
										"End Of Turn",
										JOptionPane.YES_NO_OPTION,
										JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
			player.getApplet().getMapInteraction().commitMove();
			player.callEndOfTurn();
			jBEndTurn.setEnabled(false);
		}
	}

	private void jBSendActionPerformed() {
		if (!jTFChat.getText().isEmpty()) {
			String sender = player.getCharacter().getName();
			player.sendMessage(sender, jListChat.getSelectedValue().toString(), jTFChat.getText().trim());
			if (!jListChat.getSelectedValue().toString().equals(CommonData.ALL_PLAYERS))
				sender += " to " + jListChat.getSelectedValue().toString();
			showChatMessage(sender, jTFChat.getText().trim());
			jTFChat.setText("");
		}
	}
	
	public void showChatMessage(String sender, String message) {
		cBP.showChatMessage(sender, message);
	}
	
	public void itsYourTurn() {
		JOptionPane.showMessageDialog(null,
									  "It's your turn!",
									  "Turn Change",
									  JOptionPane.INFORMATION_MESSAGE
									  );
		jBEndTurn.setEnabled(true);
	}
	
	public void showBattlePopUp() {
		JOptionPane.showMessageDialog(null,
				  "We entered Battle Mode!\n\nNew Initiative will be rolled.",
				  "Battle Mode",
				  JOptionPane.INFORMATION_MESSAGE
				  );
	}
	
	private javax.swing.JButton jBCharSheet;
	private javax.swing.JButton jBInventory;
	public javax.swing.JButton jBEndTurn;
	private javax.swing.JButton jBSend;
	private javax.swing.JLabel jLabelCharName;
	private javax.swing.JLabel jLabelCurrentHP;
	private javax.swing.JLabel jLabelHP;
	private javax.swing.JLabel jLabelTotalHP;
	private javax.swing.JList jListChat;
	private Game_Container jPGame;
	private javax.swing.JPanel jPMenus;
	private javax.swing.JPanel jPStatus;
	private javax.swing.JPanel jPanelPlayerMenu;
	private javax.swing.JScrollPane jSPParty;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JTextField jTFChat;
	private javax.swing.JTabbedPane jTPChatBox;
	private javax.swing.JTabbedPane jTPTools;
	private ChatBoxPanel cBP;

}
